package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.ActivityRemark;
import com.bjpowernode.crm.workbench.service.ActivityRemarkService;
import com.bjpowernode.crm.workbench.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : ActivityRemarkController
 * @Author : 司猫子
 * @Date: 2021/1/5 16:17
 */
@Controller
public class ActivityRemarkController {
    @Autowired
    private UserService userService;
    @Autowired
    private ActivityRemarkService activityRemarkService;
    @Autowired
    private ActivityService activityService;

    @RequestMapping("/workbench/activity/detailActivity.do")
    public String toDetailActivity(String id, Model model){
        Activity activity = activityService.selectActivityById(id);
        List<ActivityRemark> remarkList = activityRemarkService.selectActivityRemarkForDetailByActivityId(id);
        model.addAttribute("activity",activity);
        model.addAttribute("remarkList",remarkList);
        return "workbench/activity/detail";
    }


    @RequestMapping("/workbench/activity/saveCreateActivityRemark.do")
    @ResponseBody
    public Object addActivityRemark(ActivityRemark activityRemark, HttpSession session){
        User user = (User)session.getAttribute(Contants.SUSSION_USER);
        ReturnObject returnObject = new ReturnObject();
        activityRemark.setId(UUIDUtils.getUUID());
        activityRemark.setCreateTime(DateUtils.formatDateTime(new Date()));
        activityRemark.setCreateBy(user.getId());
        activityRemark.setEditFlag("0");
        int i = activityRemarkService.insertActivityRemark(activityRemark);
        if(i == 0){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("添加备注失败,请稍后重试");
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setRetData(activityRemark);
        }
        return returnObject;
    }
    @RequestMapping("/workbench/activity/saveEditActivityRemark.do")
    @ResponseBody
    public Object saveEditActivityRemark(String id,String noteContent,HttpSession session){
        User user = (User)session.getAttribute(Contants.SUSSION_USER);
        ReturnObject returnObject = new ReturnObject();
        ActivityRemark activityRemark = activityRemarkService.selectByPrimaryKey(id);
        activityRemark.setEditTime(DateUtils.formatDateTime(new Date()));
        activityRemark.setNoteContent(noteContent);
        activityRemark.setEditBy(user.getId());
        activityRemark.setEditFlag("1");
        int i = activityRemarkService.updateActivityRemark(activityRemark);
        if(i == 1){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setRetData(activityRemark);
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("更新失败,请稍后重试");
        }

        return returnObject;
    }
}
